package controllers;

import play.*;
import play.mvc.*;

import java.util.*;

import models.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import play.cache.Cache;

import siena.Column;

public class Application extends Controller {

	static String format = "yyyy/MM/dd kk:mm:ss";
	static String zone = "GMT+0";
	
    public static void index(String key, String arg) {
    	
    	TimeZone.setDefault(TimeZone.getTimeZone(zone));
    	String newFormat = Cache.get(session.getId() + "-format", String.class);
    	if(newFormat!=null){
    		format = newFormat;
    	}else{
    		Cache.set(session.getId() + "-format", format);
    	}
    	String newTimeZone = Cache.get(session.getId() + "-zone", String.class);
    	if(newTimeZone!=null){
    		zone = newTimeZone;
    	}else{
    		Cache.set(session.getId() + "-zone", zone);
    	}
    	
    	GregorianCalendar gregorianCalendar= new GregorianCalendar();
    	gregorianCalendar.setTimeZone(TimeZone.getTimeZone(zone));
    	SimpleDateFormat dateFormat = new SimpleDateFormat();
    	try{
    		dateFormat = new SimpleDateFormat(format);
    	}catch(Exception e){
    		format = "yyyy/MM/dd kk:mm:ss";
    	}
    	renderArgs.put("theZone",zone);
    	renderArgs.put("theFormat",format);
    	renderArgs.put("dateConversionNow",dateFormat.format(gregorianCalendar.getTime()));
    	renderArgs.put("millisecondsConversionNow", Long.toString(gregorianCalendar.getTime().getTime()));
    	renderArgs.put(key, arg);
        render();
    }
    
	public static void millisecondsToDate(long milliseconds) {
		Date date = new Date();
		GregorianCalendar gregorianCalendar= new GregorianCalendar();
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		
		date.setTime(milliseconds);		
		gregorianCalendar.setTimeZone(TimeZone.getTimeZone(zone));
		gregorianCalendar.setTime(date);		
		
		index("dateConversion",dateFormat.format(gregorianCalendar.getTime()));
	}
	
    public static void dateTomilliseconds(String date){
    	GregorianCalendar gregorianCalendar = new GregorianCalendar();
    	try{
    		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
    		Date d = dateFormat.parse(date);
        	
        	gregorianCalendar.setTimeZone(TimeZone.getTimeZone(zone));
        	gregorianCalendar.setTime(d);
        	
        	index("millisecondsConversion", Long.toString(gregorianCalendar.getTime().getTime()));
    	}catch(Exception e){
    		index("millisecondsConversion", "La fecha no se ha introducido en el formato adecuado.");
    		System.out.print(true);
    	}
    }
    
    public static void iniFormat(){
    	format = "yyyy/MM/dd kk:mm:ss";    	
    	Cache.set(session.getId() + "-format", format);
    	index(null, null);
    }
    
    public static void iniTimeZone(){
    	zone = "GMT+0";
    	TimeZone.setDefault(TimeZone.getTimeZone(zone));
    	Cache.set(session.getId() + "-zone", zone);
    	index(null, null);
    }
    
    public static void setFormat(String newFormat){
    	format = newFormat;
    	Cache.set(session.getId() + "-format", newFormat);
    	index(null, null);
    }
    
    public static void setTimeZone(String newTimeZone){
    	zone = newTimeZone;
    	TimeZone.setDefault(TimeZone.getTimeZone(zone));
    	Cache.set(session.getId() + "-zone", newTimeZone);
    	index(null, null);
    }
    
}